package com.example.springbootmybatis.api;

import com.example.springbootmybatis.core.redis.RedisCacheService;
import com.example.springbootmybatis.model.CompanyUser;
import com.example.springbootmybatis.service.UserService;
import com.example.springbootmybatis.tools.CodeMessage;
import com.example.springbootmybatis.tools.OperationResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
public class HelloWorldController {
    private Logger logger = LoggerFactory.getLogger(HelloWorldController.class);
    @Resource
    private UserService userService;

    @Resource
    private RedisCacheService redisCacheService;

    @RequestMapping("/hello")
    public String HelloWorld() {

        logger.error("this is hello world logger!+++++++++++++++++++++++++++++++++++++++");
        return "hello World";
    }

    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public OperationResult saveUser(@RequestBody CompanyUser user) {
        int operation = userService.saveUser(user);
//        redisCacheService.cacheCompanyUser(user.getId(),user);
        return OperationResult.buildSuccessResult(CodeMessage.S_SUCCESS);
    }

    @RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
    public OperationResult getUser(@PathVariable("userId") Integer userId) {
        CompanyUser user = userService.getUserById(userId);
        return OperationResult.buildSuccessResult(CodeMessage.S_SUCCESS,user);
    }

    @RequestMapping(value = "/user/info", method = RequestMethod.POST)
    public OperationResult getUserInfo(@RequestBody Map<String, Object> param) {
        if (param.isEmpty()) {
            return OperationResult.buildFailureResult(CodeMessage.E_ERROR);
        }
        List<CompanyUser> users = userService.getUserByIdNumberAndMobile(param.get("idNumber"),param.get("mobile"));
        return OperationResult.buildSuccessResult(CodeMessage.S_SUCCESS, users);
    }

}
